package com.zhou.common.mybatis.template.shiro;

import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.common.engine.api.delegate.event.FlowableEventType;

import static org.flowable.common.engine.api.delegate.event.FlowableEngineEventType.JOB_EXECUTION_FAILURE;
import static org.flowable.common.engine.api.delegate.event.FlowableEngineEventType.JOB_EXECUTION_SUCCESS;


/**
 * 全局添加 监听器
 * <bean id="processEngineConfiguration"
 *     class="org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration">
 *     ...
 *     <property name="eventListeners">
 *       <list>
 *          <bean class="org.flowable.engine.example.MyEventListener" />
 *       </list>
 *     </property>
 * </bean>
 *
 *
 *<bean id="processEngineConfiguration"
 *     class="org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration">
 *     ...
 *     <property name="typedEventListeners">
 *       <map>
 *         <entry key="JOB_EXECUTION_SUCCESS,JOB_EXECUTION_FAILURE" >
 *           <list>
 *             <bean class="org.flowable.engine.example.MyJobEventListener" />
 *           </list>
 *         </entry>
 *       </map>
 *     </property>
 * </bean>
 *  为流程添加 监听器
 * <process id="testEventListeners">
 *   <extensionElements>
 *     <flowable:eventListener class="org.flowable.engine.test.MyEventListener" />
 *     <flowable:eventListener delegateExpression="${testEventListener}" events="JOB_EXECUTION_SUCCESS,JOB_EXECUTION_FAILURE" />
 *   </extensionElements>
 *
 *   ...
 *
 * </process>
 */
public class MyEventListener implements FlowableEventListener {

    @Override
    public void onEvent(FlowableEvent event) {
        FlowableEventType type = event.getType();
        if (JOB_EXECUTION_SUCCESS.equals(type)) {
            System.out.println("A job well done!");
        } else if (JOB_EXECUTION_FAILURE.equals(type)) {
            System.out.println("A job has failed...");
        } else {
            System.out.println("Event received: " + event.getType());
        }
    }

    @Override
    public boolean isFailOnException() {
        // onEvent方法中的逻辑并不重要，可以忽略日志失败异常……

        return false;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }
}
